package com.uenpay.oem.ui.withdraw;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.socks.library.KLog;
import com.uenpay.im.pay.PayHandle;
import com.uenpay.oem.common.AppConstant;
import com.uenpay.oem.data.bean.UserTradePoundageInfo;
import com.uenpay.oem.data.modelImpl.BankCardModel;
import com.uenpay.oem.data.modelImpl.TransactionModel;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;

/**
 * Created by PS on 2017/6/21.
 */

public class WithdrawPresenter implements WithdrawContract.Presenter {

	private final WithdrawContract.View mWithdrawview;
	private final BankCardModel mBankCardModel;
	private final TransactionModel mTransactionModel;

	private Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			if (msg.what == 1) {
				Bundle bundle = msg.getData();
				int status = bundle.getInt("status");
				String payToken = bundle.getString("payToken");
				if (status != 0) {
					if (status == 400) {
						mWithdrawview.poundageSucced(false);
					} else {
						mWithdrawview.showToast(payToken);
					}
					return false;
				}
				double amount = bundle.getDouble("amount");
				String userBankCardId = bundle.getString("userBankCardId");
				goToWithdraw(amount, userBankCardId, payToken);
			}

			return false;
		}
	});

	public WithdrawPresenter(WithdrawContract.View view) {
		mWithdrawview = view;
		mBankCardModel = new BankCardModel();
		mTransactionModel = new TransactionModel();

	}

	@Override
	public void getTransBankCardList() {
		mBankCardModel.getTransBankList(AppConstant.TradeType.WITHDRAW, new HttpOnNextListener<TransBankCardList<BankCardInfo>>() {
			@Override
			public void onNext(TransBankCardList<BankCardInfo> bankCardList) {
				mWithdrawview.setTransBankCardList(bankCardList);
			}
		});
	}

	private double taTradeFee;

	/**
	 * 费率的成功的回调
	 * 
	 * @param tradeType
	 */
	@Override
	public void getPoundagemaney(String tradeType) {
		KLog.d(tradeType + "tradeType");
		mTransactionModel.counterFee(tradeType, new HttpOnNextListener<UserTradePoundageInfo>() {
			@Override
			public void onNext(UserTradePoundageInfo userTradePoundageInfo) {
				// 费率
				taTradeFee = Double.valueOf(userTradePoundageInfo.getTradeFee());
				mWithdrawview.getpoundage(userTradePoundageInfo);
			}

			@Override
			public void onError(Throwable e) {
				super.onError(e);

			}
		});

	}

	@Override
	public void getpayToen(final String pws, final double amount, final String userBankCardId) {
		PayHandle.getPayToken(pws, new PayHandle.IGetPayTokenResult() {
			@Override
			public void onResult(int status, String payToken) {
				KLog.d("[status=" + status + " paytoken=" + payToken);
				Bundle bundle = new Bundle();
				bundle.putInt("status", status);
				bundle.putString("payToken", payToken);
				bundle.putDouble("amount", amount);
				bundle.putString("userBankCardId", userBankCardId);
				Message msg = new Message();
				msg.what = 1;
				msg.setData(bundle);
				mHandler.sendMessage(msg);
			}

		});
	}

	private void goToWithdraw(Double amount, String userBankCardId, String payToken) {
		// 手续费 费率*金额 除以100
		final double poundage = taTradeFee * Double.valueOf(amount);
		KLog.d("round" + Math.round(poundage));
		mTransactionModel.withdraw(amount * 100 + "", userBankCardId, Math.round(poundage) + "", payToken, new HttpResultListener() {
			@Override
			public void onComplete() {
				super.onComplete();
				KLog.d("onComplete" + "成功");
				KLog.d("poundage" + poundage);
				mWithdrawview.poundageSucced(true);
			}

			@Override
			public void onError(Throwable e) {
				super.onError(e);
				KLog.d("onComplete" + "失败");
			}

		});
	}

	@Override
	public void onStart() {

	}

	@Override
	public void onDestroy() {
		mHandler = null;
	}
}
